package com.xgp.future;

import com.xgp.pc.A;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

/**
 * 异步调用
 * 异步执行
 * 成功回调
 * 失败回调
 */
public class Demo1 {
//    public static void main(String[] args) throws ExecutionException, InterruptedException {
////        //没有返回值的异步回调
////        CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
////            try {
////                TimeUnit.SECONDS.sleep(2);
////            } catch (InterruptedException e) {
////                e.printStackTrace();
////            }
////            System.out.println(Thread.currentThread().getName() + "runAsync=>Void");
////        });
////        System.out.println("111111111");
////        future.get();       //获取执行结果
////    }


    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "supplyAsync=>Integer");
            int i = 10 / 0;
            return 1024;
        });

        future.whenComplete((t,u) -> {
            System.out.println("t = " + t + " u = " + u);
        }).exceptionally((e) -> {
            System.out.println(e.getMessage());
            return 500;
        }).get();


    }
}
